package com.ytsp.ybl.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 反射工具类
 *
 * @author 常智多
 */
public class FlectUtil {
    /**
     * 根据属性名获取属性值
     */
	public static Object getFieldValueByName(String fieldName, Object o) {  
		try {    
			String firstLetter = fieldName.substring(0, 1).toUpperCase();    
			String getter = "get" + firstLetter + fieldName.substring(1);    
			Method method = o.getClass().getMethod(getter, new Class[] {});    
			Object value = method.invoke(o, new Object[] {});    
			return value;    
		} catch (Exception e) {
			return null;    
		}    
	}

    /**
     * 获取属性名数组
     */
    public static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
     */
    public static List<Map<String, Object>> getFiledsInfo(Object o) {
        Field[] fields1 = o.getClass().getDeclaredFields();
        Field[] fields2 = o.getClass().getSuperclass().getDeclaredFields();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> infoMap = null;
        for (int i = 0; i < fields1.length; i++) {
            infoMap = new HashMap<String, Object>();
            //infoMap.put("type", fields1[i].getType().toString());
            infoMap.put("name", fields1[i].getName());
            infoMap.put("value", getFieldValueByName(fields1[i].getName(), o));
            list.add(infoMap);
        }
        for (int i = 0; i < fields2.length; i++) {
            infoMap = new HashMap<String, Object>();
            //infoMap.put("type", fields2[i].getType().toString());
            infoMap.put("name", fields2[i].getName());
            infoMap.put("value", getFieldValueByName(fields2[i].getName(), o));
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 将list转str
     */
    public static String getFiledsStr(Object obj) {
        StringBuffer buffer = new StringBuffer();
        if (obj != null) {
            try {
                List<Map<String, Object>> mapList = getFiledsInfo(obj);
                if (mapList != null && mapList.size() > 0) {
                    for (Map<String, Object> map : mapList) {
                        for (Entry<String, Object> entry : map.entrySet()) {
                            buffer.append(entry.getValue());
                            buffer.append(" ");
                        }
                        //buffer.append("\r\n");
                    }
                }
            } catch (Exception e) {
            	e.printStackTrace();
                return "对象转str失败";
            }
        }
        return buffer.toString();
    }

    /**
     * 获取对象的所有属性值，返回一个对象数组
     */
    public static Object[] getFiledValues(Object o) {
        String[] fieldNames = getFiledName(o);
        Object[] value = new Object[fieldNames.length];
        for (int i = 0; i < fieldNames.length; i++) {
            value[i] = getFieldValueByName(fieldNames[i], o);
        }
        return value;
    }
}
